********************************************************************************
*                    DATASET CREATION REPLICATION DO FILE                      *    
*                                    for                                       * 
*                                                                              *
*    Triggering Populist Support in Europe: How Internal Political Efficacy    * 
*       Shapes the Effect of Economic Vulnerability on Party Preferences       *
*                                                                              *
*                       European Political Studies                             *
*                                                                              *
*               Marija Verner, PhD (marija.verner@yale.edu)                    *
*                Pavel Bacovsky, PhD (pbacovsky@bates.edu)                     * 
*           Sarah Wilson Sokhey, PhD (sarah.sokhey@colorado.edu)               *
********************************************************************************

* Last updated: October 2023

* Set directory for output files
cd /*filepath*/

* Working dataset is "ESS_Dataset_Clean.dta" (open manually or change filepath as needed)
use /*filepath*/, clear

* Log (change replace or append as needed)
capture log close
log using /*filepath*/, append

* Install any of the following packages, as needed
ssc install corrtex

//////////////////////////// SUMMARY STATISTICS	///////////////////////////	

*********************** TABLE 1 ***********************

* Limit the sample to only what's been included in the regression.

	* 1. Run the main model.
	egen pan_id = group(cntry_num cseqno)
	xtset pan_id essround		
	xtlogit pop_vote inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	
	* 2. Generate a dummy variable for observations used in the regression. After running the regression, Stata automatically creates a system variable called "e(sample)" that marks with 1 the observations used in the last estimation and 0 otherwise. Create a copy of this variable called "used".
	gen byte used = e(sample)
	
	* 3. Calculate summary stats
	tab pop_vote if used == 1
	tab pop_right if used == 1
	tab pop_left if used == 1
	tab pop_close if used == 1
	tab pcl_right if used == 1
	tab pcl_left if used == 1

	sum pop_vote if used == 1
	sum pop_right if used == 1
	sum pop_left if used == 1
	sum pop_close if used == 1
	sum pcl_right if used == 1
	sum pcl_left if used == 1
	sum inteffidx_res if used == 1 
	sum exteffidx if used == 1 
	sum instrustidx if used == 1 
	sum euftf if used == 1 
	sum gincdif if used == 1 
	sum lrscale if used == 1 
	sum polintr if used == 1 
	sum hinctnta if used == 1 
	sum i.uemp3m if used == 1 
	sum edulvla if used == 1 
	sum i.gndr if used == 1 
	sum agea if used == 1 
	sum age_sq if used == 1


///////////////////////////////// MODELS  /////////////////////////////////

******************************* MAIN MODELS *******************************	
************************ TABLE 2 & FIGURES 1 & SM1 ************************
	
egen pan_id = group(cntry_num cseqno)
xtset pan_id essround		

* What explains populist party vote? 
		
	* Model 1 (direct effects only)
	xtlogit pop_vote inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m1
	
	* Model 2 (hypothesizing that the effect of income depends on internal political efficacy)
	xtlogit pop_vote c.hinctnta##c.inteffidx_res i.uemp3m##c.inteffidx_res inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m2
	
		* Figure 1A: Effect of income on a populist vote moderated by internal political efficacy
		margins, at(hinctnta=(0(2)10) inteffidx_res=(2 10))
		marginsplot, recast(line) recastci(rarea) yline(0)	
		graph save pop_vote_internal_efficacy_income
		
		* Figure SM1A: Effect of unemployment on a populist vote moderated by internal political efficacy
		margins, dydx(uemp3m) at(inteffidx_res=(2 4 6 8 10)) vsquish 
		marginsplot, recast(line) recastci(rarea) yline(0)	
		graph save pop_vote_internal_efficacy_unemployment

* What explains closeness to a populist party? 

	* Model 3 (direct effects only)
	xtlogit pop_close inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m3	
	
	* Model 4 (hypothesizing that the effect of income depends on internal political efficacy)
	xtlogit pop_close c.hinctnta##c.inteffidx_res i.uemp3m##c.inteffidx_res inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m4
	
		* Figure 1B: Effect of income on closeness to populist party moderated by internal political efficacy
		margins, at(hinctnta=(0(2)10) inteffidx_res=(2 10))
		marginsplot	
		graph save pop_close_internal_efficacy_income

		* Figure SM1B: Effect of unemployment on closeness to populist party moderated by internal political efficacy
		margins, dydx(uemp3m) at(inteffidx_res=(2 4 6 8 10)) vsquish 
		marginsplot, recast(line) recastci(rarea) yline(0)
		graph save pop_close_internal_efficacy_unemployment

* export table, don't forget to set a filepath				
outreg2 [m1 m2 m3 m4] using /*filepath*/, se dec(3) replace

*************** RIGHT & LEFT POPULIST PARTIES: VOTING MODELS ***************	
********************** TABLE 3 & FIGURES 2 & SM2 ***************************

* What explains populist right party vote? 

	* Model 1
	xtlogit pop_right inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m1
	
	* Model 2 (hypothesizing that the effect of income depends on internal political efficacy)
	xtlogit pop_right c.hinctnta##c.inteffidx_res i.uemp3m##c.inteffidx_res inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m2
	
		* Figure 2A: The effect of income on voting for a populist right party moderated by internal political efficacy.
		margins, at(hinctnta=(0(2)10) inteffidx_res=(2 10))
		marginsplot
		graph save pop_right_internal_efficacy_income

		* Figure SM2A: The effect of unemployment on voting for a populist right party moderated by internal political efficacy.
		margins, dydx(uemp3m) at(inteffidx_res=(2 4 6 8 10)) vsquish 
		marginsplot, recast(line) recastci(rarea) yline(0)	
		graph save pop_right_internal_efficacy_unemployment

* What explains populist left party vote? 
	* Model 3
	xtlogit pop_left inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m3
	
	* Model 4 (hypothesizing that the effect of income depends on internal political efficacy)
	xtlogit pop_left c.hinctnta##c.inteffidx_res i.uemp3m##c.inteffidx_res inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m4
	
		* Figure 2B: The effect of income on voting for a populist left party moderated by internal political efficacy.
		margins, at(hinctnta=(0(2)10) inteffidx_res=(2 10))
		marginsplot
		graph save pop_left_internal_efficacy_income
		
		* Figure SM2B: The effect of unemployment on voting for a populist left party moderated by internal political efficacy.
		margins, dydx(uemp3m) at(inteffidx_res=(2 4 6 8 10)) vsquish 
		marginsplot, recast(line) recastci(rarea) yline(0)	
		graph save pop_left_internal_efficacy_unemployment
	
* export table, don't forget to set a filepath				
outreg2 [m1 m2 m3 m4] using /*filepath*/, replace se dec(3)

************** RIGHT & LEFT POPULIST PARTIES: CLOSENESS MODELS **************	
********************* TABLE 4 & FIGURES 2 & SM2 *****************************

* What explains closeness to a populist right party? 

	* Model 1
	xtlogit pcl_right inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m1
	
	* Model 2 (hypothesizing that the effect of income depends on internal political efficacy)
	xtlogit pcl_right c.hinctnta##c.inteffidx_res i.uemp3m##c.inteffidx_res inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m2
	
		* Figure 2C: The effect of income on feeling close to a populist right party moderated by internal political efficacy.
		margins, at(hinctnta=(0(2)10) inteffidx_res=(2 10))
		marginsplot
		graph save pcl_right_internal_efficacy_income
	
		* Figure SM2C: The effect of unemployment on feeling close to a populist right party moderated by internal political efficacy.
		margins, dydx(uemp3m) at(inteffidx_res=(2 4 6 8 10)) vsquish 
		marginsplot, recast(line) recastci(rarea) yline(0)	
		graph save pcl_right_internal_efficacy_unemployment

* What explains closeness to a populist left party? 

	* Model 3
	xtlogit pcl_left inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m3
	
	* Model 4 (hypothesizing that the effect of income depends on internal political efficacy)
	xtlogit pcl_left c.hinctnta##c.inteffidx_res i.uemp3m##c.inteffidx_res inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	eststo m4
	
		* Figure 2D: The effect of income on feeling close to a populist left party moderated by internal political efficacy.
		margins, at(hinctnta=(0(2)10) inteffidx_res=(2 10))
		marginsplot
		graph save pcl_left_internal_efficacy_income
	
		* Figure SM2D: The effect of unemployment on feeling close to a populist left party moderated by internal political efficacy.
		margins, dydx(uemp3m) at(inteffidx_res=(2 4 6 8 10)) vsquish 
		marginsplot, recast(line) recastci(rarea) yline(0)	
		graph save pcl_left_internal_efficacy_unemployment
	
* export table, don't forget to set a filepath				
outreg2 [m1 m2 m3 m4] using /*filepath*/, se dec(3) replace

///////////////////////// ROBUSTNESS CHECKS /////////////////////////

egen pan_id = group(cntry_num cseqno)
xtset pan_id essround		

* What explains populist party vote? 
		
	* Model 1 (direct effects only)
	xtlogit pop_vote inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta hincfel i.uemp3m edulvla i.gndr agea age_sq, re
	
	* Model 2 (hypothesizing that the effect of income depends on internal political efficacy)
	xtlogit pop_vote c.hinctnta##c.inteffidx_res c.hincfel##c.inteffidx_res i.uemp3m##c.inteffidx_res inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re

		* Figure: Effect of subjective income on a populist vote moderated by internal political efficacy
		margins, at(hincfel=(0(1)4) inteffidx_res=(2 10))
		marginsplot	
		graph save pop_vote_internal_efficacy_subj_income

* Does economic insecurity contribute to low institutional trust?

xtlogit instrustidx hinctnta i.uemp3m inteffidx_res exteffidx euftf gincdif lrscale polintr edulvla i.gndr agea age_sq, re

xtlogit instrustidx c.hinctnta##c.inteffidx_res i.uemp3m##c.inteffidx_res hinctnta i.uemp3m inteffidx_res exteffidx euftf gincdif lrscale polintr edulvla i.gndr agea age_sq, re

	* Figure: Effect of income on institutional trust moderated by internal political efficacy
	margins, at(hinctnta=(0(1)4) inteffidx_res=(2 10))
	marginsplot	
	graph save inst_trust_internal_efficacy_income
	
	* Figure: Effect of unemployment on institutional trust moderated by internal political efficacy
	margins, dydx(uemp3m) at(inteffidx_res=(2 4 6 8 10)) vsquish 
	marginsplot, recast(line) recastci(rarea) yline(0)	
	graph save inst_trust_internal_efficacy_unemployment

///////////////////////////// ADDITIONAL ANALYSIS IN RESPONSE TO REVIEWS /////////////////////////////	
	
* Breaking things down with the countries

cd "/*filepath*/"                                                               // Make sure you set up your working directory to whatever you need it to be.

drop if cntry == "LV"                                                           // LV broke the code, so it needs to be dropped for the whole loop to finish.
levelsof cntry, local(levels)
foreach l of local levels {
	quietly xtlogit pop_vote c.hinctnta##c.inteffidx_res i.uemp3m##c.inteffidx_res inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq if cntry == "`l'", re
	quietly eststo m_`l'
	margins, at(hinctnta=(0(2)10) inteffidx_res=(2 10))
	marginsplot, recast(line) recastci(rarea) yline(0)	
	graph save `l'_pop_vote_internal_efficacy_income
}
* Explore unemployment 

	xtlogit pop_vote inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr i.uemp3m edulvla i.gndr agea age_sq, re
	
	xtlogit pop_close inteffidx_res exteffidx instrustidx euftf gincdif lrscale polintr i.uemp3m edulvla i.gndr agea age_sq, re

* Response to R2's request for analysus using institutional trust as the DV
	* Model 1 run with *instrustidx* as DV
	xtlogit instrustidx inteffidx_res exteffidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	
	* Model 2 (hypothesizing that the effect of income depends on internal political efficacy) run with *instrustidx* as DV
	xtlogit instrustidx c.hinctnta##c.inteffidx_res i.uemp3m##c.inteffidx_res inteffidx_res exteffidx euftf gincdif lrscale polintr hinctnta i.uemp3m edulvla i.gndr agea age_sq, re
	
		* Figure: Effect of income on a populist vote moderated by internal political efficacy
		margins, at(hinctnta=(0(2)10) inteffidx_res=(2 10))
		marginsplot, recast(line) recastci(rarea) yline(0)
		graph save inst_trust_internal_efficacy_income.gph
				
		* Figure: Effect of unemployment on a populist vote moderated by internal political efficacy
		margins, at(uemp3m=(0(1)1) inteffidx_res=(2 10)) 
		marginsplot, recast(line) recastci(rarea) yline(0)	
		graph save inst_trust_internal_efficacy_unemp.gph
		



